      SUBROUTINE TGLOC06(NTARGS,TARGNAME,NPARMS,PARMS,SCPOS,
     *                KSYSNAT,KCENNAT,TARGPOS,LUERR,IERR)
      IMPLICIT REAL*8 (A-H,O-Z)
C
C THIS ROUTINE IS PART OF THE TOSS TARGET LOCATION PACKAGE, TGLOC.  IT
C COMPUTES THE POSITION VECTOR(IN LOCAL ORBITAL COORDINATES) OF A
C HORIZON POINT DEFINED BY ITS ALTITUDE ABOVE THE EARTH AND ITS AZIMUTH
C IN LOCAL ORBITAL COORDINATES.  NOTE THAT A SPHERICAL EARTH IS ASSUMED.
C
C
C VAR     DIM    TYPE I/O DESCRIPTION
C ---     ---    ---- --- -----------
C
C NTARGS   1      I*4  I  NUMBER OF TARGETS TO BE PROCESSED.
C                         IF ZERO OR NEGATIVE, NONE ARE PROCESSED.
C
C TARGNAME NTARGS CH*16 I TARGET NAMES;  USED FOR ERROR MESSAGES,
C                         OTHERWISE THE NAME HAS NO SIGNIFICANCE.
C
C NPARMS   1      I*4  I  THE NUMBER OF PARAMETERS SUPPLIED IN THE PARMS
C                         ARRAY.  MUST BE 3 OR GREATER.
C
C PARMS           R*8  I  SAME AS TGLOC'S DESCRIPTION OF PARMS FOR
C    NPARMS,NTARGS        TARGET TYPE 6.
C
C SCPOS    3      R*8  I  THE SPACECRAFT POSITION VECTOR.  COORDINATE
C                         SYSTEM IS GEOCENTRIC, MEAN EARTH EQUATOR AND
C                         EQUINOX OF 1950.0;  IN KM.
C
C KSYSNAT  1      I*4  O  IDENTIFIES THE NATURAL COORDINATE SYSTEM'S
C                         ORIENTATION.  IS SET TO 12 TO INDICATE THE
C                         ORIENTATION IS LOCAL ORBITAL.
C
C KCENNAT  1      I*4  O  IDENTIFIES THE NATURAL COORDINATE SYSTEM'S
C                         ORIGIN.  IS SET TO 3 TO INDICATE THE ORIGIN IS
C                         AT THE SPACECRAFT; IE, AT SCPOS.
C
C TARGPOS 3,NTARGS R*8 O  TARGPOS(-,I) IS THE POSITION VECTOR OF THE
C                         I'TH TARGET.  IN GEOCENTRIC MEAN EARTH
C                         EQUATOR AND EQUINOX OF 1950.0;  IN KM.
C
C LUERR    1      I*4   I FORTRAN UNIT NUMBER FOR ERROR MESSAGES.
C                          = 0/NEGATIVE, NO MESSAGES POSSIBLE.
C
C IERR     1      I*4   O ERROR RETURN FLAG.
C                          = 0, NO ERROR.
C                          = OTHERWISE, ERROR.
C
C***********************************************************************
C
C BY C PETRUZZO, GSFC/742, 8/85.
C        MODIFIED....
C
C***********************************************************************
C
      REAL*8 PARMS(NPARMS,1),SCPOS(3),TARGPOS(3,1),TPOS(3)
      REAL*8 HALFPI/ 1.570796326794897D0 /
      CHARACTER*16 TARGNAME(1)
C
      IERR = 0
      KSYSNAT = 12    ! ORIENTATION IS LOCAL ORBITAL
      KCENNAT = 3     ! ORIGIN IS AT THE S/C
      IF(NTARGS.LE.0) GO TO 9999
C
C GET TARGET POSITION VECTOR IN 'NATURAL' SYSTEM. SPHERICAL EARTH.
C
      REARTH = CONST(53)
      EFLAT =  CONST(59)
      RAD =    FNORM(SCPOS)     ! S/C DISTANCE FROM EARTH CENTER
C
      DO 1000 ITARG = 1,NTARGS
C
      CALL TGLOC06A(NPARMS,PARMS(1,ITARG),TARGNAME(ITARG),
     *         LUERR,IERR1)
      IF(IERR1.NE.0) THEN
        IERR = 1
        TARGPOS(1,ITARG) = 'ERROR'
        TARGPOS(2,ITARG) = 'ERROR'
        TARGPOS(3,ITARG) = 'ERROR'
        GO TO 1000
        END IF
C
      AZ =     PARMS(1,ITARG)
      ALT =    PARMS(2,ITARG)
      FLAT =   PARMS(3,ITARG)
      IF(FLAT.NE.0.D0) FLAT = EFLAT
C
C SPHERICAL EARTH
C
      IF(FLAT.EQ.0.D0) THEN
        SINEL = (REARTH+ALT)/RAD
        IF(SINEL.GE.0.D0 .AND. SINEL.LE.1.D0) THEN
          EL = HALFPI-DASIN( SINEL )  !ELEVATION IN LOCAL ORBITAL COORDS
          DIST = DSQRT( RAD**2 - (REARTH+ALT)**2 )  !DISTANCE TO HORIZON
          CALL XYZSPH(1,AZ,EL,DIST,TPOS,1.D0)
        ELSE
          IERR = 1
          TARGPOS(1,ITARG) = 'ERROR'
          TARGPOS(2,ITARG) = 'ERROR'
          TARGPOS(3,ITARG) = 'ERROR'
          IF(LUERR.GT.0) WRITE(LUERR,1001) RAD-REARTH,ALT
 1001     FORMAT(/,' TGLOC06 ERROR. S/C IS BELOW HORIZON ALTITUDE'/,
     *             '     S/C ALT=',G16.8,' HORIZON ALT=',G16.8/)
          GO TO 1000
          END IF
        END IF
C
C OBLATE EARTH
C
      IF(FLAT.NE.0.D0) THEN
        STOP 'TGLOC06.  ERROR END.  OBLATE EARTH NOT AVAILABLE.'
        END IF
C
      TARGPOS(1,ITARG) = TPOS(1)
      TARGPOS(2,ITARG) = TPOS(2)
      TARGPOS(3,ITARG) = TPOS(3)
C
 1000 CONTINUE
C
 9999 CONTINUE
      RETURN
      END
